package com.party.service.apply.impl;

import com.party.common.Page;
import com.party.mapper.ApplyMapper;
import com.party.mapper.CurriculumMapper;
import com.party.model.*;
import com.party.service.apply.ApplyService;
import com.party.service.study.CurriculumService;
import com.party.service.study.User2TestService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 011926 on 2014/9/23.
 */
@Repository
public class ApplyServiceImpl implements ApplyService
{
    @Resource
    ApplyMapper applyMapper;


    @Override
    public int add(Apply apply) {
        if(apply != null)
        {
            return this.applyMapper.insertSelective(apply);
        }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        if(id != null && id > 0)
        {
            return this.applyMapper.deleteByPrimaryKey(id);
        }
        return 0;
    }

    @Override
    public Apply getById(Integer id) {
        if(id != null && id > 0 )
        {
            return this.applyMapper.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    public Page getList(Apply apply, int pageSize, int pageNumber) {

        ApplyExample example = new ApplyExample();
        if(apply != null)
        {
            ApplyExample.Criteria criteria = example.createCriteria();
            if(apply.getTitle() != null && !"".equals(apply.getTitle()))
            {
                criteria.andTitleLike("%"+apply.getTitle()+"%");
            }
            if(apply.getContent() != null && !"".equals(apply.getContent()))
            {
                criteria.andContentLike("%"+apply.getContent()+"%");
            }
            if(apply.getRealname()!=null && !"".equals(apply.getRealname()))
            {
                criteria.andRealnameLike("%"+apply.getRealname()+"%");
            }
            if(apply.getStatus() != null && !"".equals(apply.getStatus()))
            {
                criteria.andStatusEqualTo(apply.getStatus());
            }
            //不判断是否大于0，因为我有可能故意然uid=-1，让其查不出来（在用户还未登陆的情况下）
            if(apply.getUid() != null)
            {
                criteria.andUidEqualTo(apply.getUid());
            }
        }
        //获取总的记录数量
        int count = this.applyMapper.countByExample(example);
        //获取指定的页码数据
        example.setBeginNum((pageNumber - 1) * pageSize);
        example.setEndNum(pageSize);
        List<Apply> list = this.applyMapper.getListPage(example);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);
        return page;
    }

    @Override
    public int edit(Apply apply) {
        if(apply != null && apply.getId() != null && !"".equals(apply.getId()))
        {
            return this.applyMapper.updateByPrimaryKeySelective(apply);
        }
        return 0;
    }

    @Override
    public List<Apply> getAll() {
        ApplyExample example = new ApplyExample();
        return this.applyMapper.selectByExample(example);
    }
}
